<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Session;
use think\facade\Request;
use think\Db;

class Common extends Controller
{
    private $_development_display = true; //开发显示设置

    protected $beforeActionList = [
        'setDeveloper', //设置开发者基本的参数
    ];

    // 公共
    public function initialize()
    {
        if (!Session::has('admin', 'mmjtAdmin')) {
            $this->redirect('login/index');
        } else {
            $adminId = Session::get('admin', 'mmjtAdmin');
            $adminId = Db::name('admin')->where(['id' => $adminId['id'], 'name' => $adminId['name'], 'type' => $adminId['type'], 'hide' => $adminId['hide'], 'password' => $adminId['password']])->find();
            if (!$adminId) {
                session(null, 'mmjtAdmin');
                $this->redirect('login/index');
            } else {
                if ($adminId['id'] != 1 && $adminId['type'] != 1) {

                    $controller = strtolower(Request::controller());
                    $action = strtolower(Request::action());

                    if ($controller == 'index' && in_array($action, ['index', 'out', 'main', 'cache_del'])) return;

                    $category_id = Db::name('category')->where(['controller' => $controller, 'function' => $action])->value('id');
                    $data = Db::name('admin_type')->where(['id' => $adminId['type']])->value('data');

                    if (!($category_id && $data)) $this->error('菜单不存在');

                    $data = json_decode($data, true);
                    if (!in_array($category_id, $data)) $this->error('无权访问');
                }
            }

        }
    }

    /**
     * User:Simony
     * Function:修改状态
     */
    public function changeTableVal()
    {
        if ($this->request->isAjax()) {

            //params
            $table = input('post.table');
            $id_name = input('post.id_name');
            $id_value = input('post.id_value');
            $field = input('post.field');
            $value = input('post.value');
            $msg = input('post.msg');

            $where = [$id_name => $id_value];
            $update = [$field => $value];

            //getdata
            $data = Db::name($table)->where($where)->find();
            if (empty($data)) ds_json(0, '数据不存在');

            if ($data[$field] == $value) ds_json(0, '数据已处理，请勿重复操作');


            $res = Db::name($table)->where($where)->update($update);
            $res ? ds_json(1, $msg . '成功') : ds_json(0, '操作失败，请再试一次');

        }
        ds_json(0, '请求方式有误');
    }

    /**
     * User:Simony
     * Function: 常规删除
     */
    public function delById()
    {

        if ($this->request->isAjax()) {

            //params
            $id = input('post.id/d', '');
            $table = input('post.table');

            //judge
            if (!$id) ds_json_encode('err', '缺少必要参数');

            $data = Db::name($table)->where('id', $id)->find();
            if (!$data) ds_json_encode('err', '数据不存在');

            if (isset($data['is_sys']) && $data['is_sys'] == 1) ds_json_encode('ret', '系统数据不能删除');

            //data
            $res = Db::name($table)->delete($id);

            $res ? ds_json_encode('ret', '删除成功') : ds_json_encode('err', '删除失败');
        }
        ds_json_encode('err', '请求方式有误');
    }


    /**
     * User:Simony
     * Function:设置开发者基本参数
     */
    protected function setDeveloper()
    {
        $this->_development_display = \think\facade\Config::get('developer.display_param');
        $this->assign('development_display', $this->_development_display);
    }
}